/*  
 * This class is the core class responsible for The Disk Benchmarking software.
 * It will track the file writer threads, and create and maintain the random 
 * hastables used for generating files.
 */


package filetest.filewriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileWriter{
    //The below variable is responsible for maintaining the random Hash.
    private static final int ITERATIONS = 1200;
    private static volatile ConcurrentHashMap  <Integer,String> fileset = 
            new ConcurrentHashMap <Integer, String>();
    private static String directory;
    private volatile Calendar calendar = null;
    private volatile SimpleDateFormat sdf = null;
    private ExecutorService executorService= null;
    private int cores = 0;
    private int threadstoExecute = 0;
    private FileWriterThreadObj objectWritingFile= null;
    @Override
    public String toString() {
        return "FileWriter [getDirectory()=" + getDirectory()
                        + ", getDateTime()=" + getDateTime() + ", getCores()="
                        + getCores() + "]";
    }
    public FileWriter(String directory){
        this.setCores(Runtime.getRuntime().availableProcessors());
        this.calendar = Calendar.getInstance();
        this.sdf = new SimpleDateFormat("yyyy-MM-dd'|'HH:mm:ss|z");
        FileWriter.directory = directory;
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < ITERATIONS; i++){
                for (int d = 0; d <2048; d++){
                        temp.append(this.getRandomCharacter());
                }
                FileWriter.setCharFileSet(i, temp.toString());
                temp.setLength(0);
        }

    } 
    public static void setCharFileSet(int dicidentifier, String hash){
            fileset.put(dicidentifier,hash);
    }
    public static String getRandomCharacterString (int i){
            return fileset.get(i);
    }
    public static void setDirectory(String directory) {
            FileWriter.directory = directory;
    }
    public char getRandomCharacter (){
            Random r = new Random();
            char c = (char)(r.nextInt(26)+'a');
            return c;
    }
    public String getDirectory(){
            return FileWriter.directory;
    }
    public String getDateTime(){
            return this.sdf.format(this.calendar.getTime());
    }
    public int getCores() {
            return cores;
    }
    //The setCores method will also set the thread execution pool size by 
    //calling the setExecutorServicePool method. 
    public void setCores(int cores) {
            this.cores = cores;
            this.setExecutorServicePool(this.getExecutorService());
    }
    public ExecutorService getExecutorService() {
            return executorService;
    }
    private void setExecutorServicePool(ExecutorService executorService) {
            int temp= 0;
            for (int i= 0; i < this.getCores(); i++){
                    temp = 4 + temp;

            }
            this.setThreadstoExecute(temp);
            this.executorService = Executors.newFixedThreadPool
                    (this.getThreadstoExecute());

    }
    public void setExecutorServiceThreadToExcute(FileWriterThreadObj obj){
            //Waiting for code here.
    }
    public int getThreadstoExecute() {
            return threadstoExecute;
    }
    private void setThreadstoExecute(int threadstoExecute) {
            this.threadstoExecute = threadstoExecute;
    }
    public FileWriterThreadObj getObjectWritingFile() {
            return objectWritingFile;
    }
    public void setObjectWritingFile(FileWriterThreadObj objectWritingFile) {
            this.objectWritingFile = new FileWriterThreadObj(this);
    }
    public static int getITERATIONS(){
        return FileWriter.ITERATIONS;
    }

}
